package com.foreknow.elm.controller;

import com.foreknow.elm.po.Cart;
import com.foreknow.elm.service.CartService;
import com.foreknow.elm.service.impl.CartServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class CartController {

    /**
     * 功能：根据用户编号查询此用户所有购物车信息
     * ​           根据用户编号和商家编号，查询此用户购物车中某个商家的所有购物车信息
     *
     * @param request
     * @return
     */
    public Object listCart(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        String businessId = request.getParameter("businessId");
        Cart cart = new Cart();
        cart.setUserId(userId);
        if (businessId != null) {
            cart.setBusinessId(Integer.valueOf(businessId));
        }
        CartService cartService = new CartServiceImpl();
        List<Cart> list = cartService.selectCart(cart);
        return list;
    }

    /**
     * 功能：向购物车表中添加一条记录
     *
     * @param request
     * @return
     */
    public Object saveCart(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        String businessId = request.getParameter("businessId");
        String foodId = request.getParameter("foodId");
        CartService cartService = new CartServiceImpl();
        Integer i = cartService.saveCart(userId, Integer.valueOf(businessId), Integer.valueOf(foodId));
        return i;
    }

    /**
     * 功能：根据用户编号、商家编号、食品编号更新数量
     *
     * @param request
     * @return
     */
    public Object updateCart(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        String businessId = request.getParameter("businessId");
        String foodId = request.getParameter("foodId");
        String quantity = request.getParameter("quantity");
        CartService cartService = new CartServiceImpl();
        Integer i = cartService.updateCart(userId, Integer.valueOf(businessId), Integer.valueOf(foodId), Integer.valueOf(quantity));
        return i;
    }

    /**
     * 根据用户编号、商家编号、食品编号删除购物车表中的一条食品记录
     * ​           根据用户编号、商家编号删除购物车表中的多条条记录
     *
     * @param request
     * @return
     */
    public Object removeCart(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        String businessId = request.getParameter("businessId");
        String foodId = request.getParameter("foodId");
        Cart cart = new Cart();
        cart.setUserId(userId);
        cart.setBusinessId(Integer.valueOf(businessId));
        if (foodId != null) {
            cart.setFoodId(Integer.valueOf(foodId));
        }
        CartService cartService = new CartServiceImpl();
        Integer i = cartService.removeCartOnly(cart);
        return i;
    }

}
